$import("biz.base.FormEditorUtil"); 

(function(exports){
	function TreeFormPanel(c){
		var me=this,initialConfig=me.initialConfig=c;
		Ext.applyIf(me,c);
	}
	var pt=TreeFormPanel.prototype;
	pt={
			initConfig:function(){
				var me=this,me.tabPanelId="detailTabPanel"+me.id;
				this.dataUrl=this.dataUrl||'./meta/dataTree?rankId='+me.id;
				this.editor={id:1,text:"",url:""};
			},
			initTreeConfig:function(region){
				var me=this,ctrlId=this.treecptId,
				treeLoader = new Ext.tree.TreeLoader({
					dataUrl :me.dataUrl,
					nodeParameter:"id"
				});
				this.treeConfig={
						id : ctrlId,
						split : true,
						region : region,
						autoScroll : true,
						width : 350,
						enableDD:true,
						animate : true,
						floatable : false,
						titleCollapse : true,
						rootVisible : false,
						lines : false,
						useArrows : true,
						loader : treeLoader,
						root : new Ext.tree.AsyncTreeNode({
							id : me.rootId,
							text : '根节点'
						}),
						tbar:[{
							iconCls:'icon-create',
							tooltip:{text:'新建'},
							text:'新建',
							handler:function(){
								var metaTree=Ext.getCmp(ctrlId);
								var cur_node=metaTree["cur_node"];
								var win=me.getNewNodeWin();
								win.show();
							}
						},{
							text:'删除',
							iconCls:'icon-delete',
							handler:function(){
								var metaTree=Ext.getCmp(ctrlId);
								if(!metaTree["cur_node"]){
									return Ext.Msg.alert('提示','请选择要删除的节点');
								}
								Ext.Msg.confirm('提示',"确认删除此项(确保无子节点)?",function(txt){
									if(txt=='no')return;
									
									var pNode=metaTree["cur_node"].parentNode;
									var url="./delete/"+metaTree["cur_node"].id;
									doHttpGet(url);
									pNode.reload();
								});
							}
						},{
							text:'刷新',
							iconCls:'icon-reload',
							handler:function(){
								var metaTree=Ext.getCmp(ctrlId);
								metaTree.getRootNode().reload();
								window.parent.reloadMenu();
							}
						}],
						listeners:{
							'click':function(n){
								metaTree["cur_node"]=n;
								var detailForm=me.tabPanel,editor=me.editor;
								tabPanel.showTab(editor.text,editor.id,editor.url+"?id="+n.id);
							},
							'movenode':function(tree,node,oldParent,newParent,index){
								var url=getContextBase()+"meta/move";
								Ext.Ajax.request( {
										url : url,
										method : 'post',
										params : {
											id:node.id,
											fid:newParent.id,
											seq:index,
											rankId:rankId
										},
										success: function() { 
											oldParent.reload();
											if(oldParent!=newParent)
												newParent.reload();
						                },
						                failure: function() {  
						                	oldParent.reload();
											if(oldParent!=newParent)
												newParent.reload();
						                }  
								    });
							}
						}
					};
			},
		initTree:function(region){
			var me=this,config=me.initTreeConfig(region),
			metaTree =this.treePanel=new Ext.tree.TreePanel(config);
			return metaTree;
		},
		getFieldByMeta:function(field){
			var attr={},attrs = field["attrs"];
			if(attrs.required){attr.allowBlank=true;}
			Ext.applyIf(attr,field);
			delete attr["attrs"];
			var fld= FormEditorUtil.get(field['type'],attr);
			if( fld.fieldLabel && fld.allowBlank===false ){
				fld.fieldLabel += '<font color=red>*</font>';
			}
			return fld;
		},
		getNewNodeWin:function(){
			var me=this,id='addNodeWin'+this.id, win=Ext.getCmp(id);
			if(!win){
				var form=new Ext.form.Panel({
					jsonSubmit:true,
					url:me.addRecordUrl,
					defaults:{
						width:190,
						xtype:'textfield'
					},
					layout:'border',
					items:me.getAddFormFields()
				});
				win=new Ext.Window({
					id:id,
					title:"新建",
					layout:'fit',
					width:220,
					height:200,
					items:form,
					buttons:[
						{
							text:"提交",
							handler:function(){
								
								var win=Ext.getCmp(id);
								win.destroy();
							}
						},{
							text:"取消",
							handler:function(){
								var win=Ext.getCmp(id);
								win.destroy();
							}
						}
					]
				});
			}
			return win.show();
		},
		getAddFormFields:function(){
			var items=this.getEditFormFields();
			return items;
		},
		getEditFormFields:function(){
			var me=this,fields=me["fields"],fs=[];
			Ext.each(fields,function(field,i){
				var fld=me.getFieldByMeta(field);
				fs.push(fld);
			});
			return fs;
		},
		initTabPanel:function(region){
				var me=this,tabPanel=this.tabPanel=new Ext.TabPanel({
			        id:me.tabPanelId,
			        region:region,
			        split:true,
			        deferredRender:false,
			        enableTabScroll:true,
			        autoScroll: true,
			        autoWidth:false,
			        activeTab:0,
			        bodyCssClass :'bgFilter',
			        items:[]
				});
				tabPanel.layoutTabs=new HashMap();
				
				tabPanel.showTab = function(title,id,page){
					var layoutTabs=tabPanel.layoutTabs;
					if(!title)title=id;
					var tid = id || ++Ext.Component.AUTO_ID;
					var myTab=layoutTabs.get(tid);	
					if(!myTab ){
						var path='';
						if( path.substr(0,1)=="."|| path.substr(0,1)=="/"|| path.substr(0,4)=="http"){
							
						}else{
							path = '../meta/'+page+'.jsp?id='+id;
						}
						var tConf = {title:title, closable:true,autoScroll:true };
							tConf.html='<IFRAME src="'+path+'" frameborder="0" width="100%" height="100%" scrolling="auto" />';
						myTab = tabPanel.add(tConf);
						layoutTabs.put(tid,myTab);
						myTab.on("destroy",function(){layoutTabs.remove(tid);});
					}
					myTab.show();
					return myTab;
				};
				return tabPanel;
		},
		showAddNodeWin:function(){
			
		},
		doReload:function(id){
			
		},
		doDeleteNode:function(){
			
		},
		display:function(){
			var me=this;
			Ext.onReady(function() {
				Ext.QuickTips.init();
				var tree=me.initTree('west');
				var detailForm=me.initTabPanel('center');
				new Ext.Viewport({
					layout : 'border',
					items : [tree, detailForm]
				});
			});
		}
	};
	exports.TreeTabPanel=TreeTabPanel;
})(window);
new TreeTabPanel(metaData);
